<template>
    <div class="container">
        <el-row>
            <el-form ref="form" :rules="rules" :model="form" label-width="200px" label-position="right">
                <el-form-item label="姓名：" prop="name" style="width: 500px">
                    <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="客户来源：" prop="source" style="width: 500px">
                    <el-input v-model="form.source" placeholder="请输入客户来源"></el-input>
                </el-form-item>
                <el-form-item class="radio-form" label="是否添加企微：" prop="addWxWork" style="width: 500px">
                    <el-radio-group v-model="form.addWxWork">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="0">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item prop="phone" label="联系方式：" style="width: 500px">
                    <el-input v-model="form.phone" placeholder="请输入联系方式"></el-input>
                </el-form-item>
                <el-form-item label="身份证：" prop="idcardNo" style="width: 500px">
                    <el-input v-model="form.idcardNo" placeholder="请输入身份证"></el-input>
                </el-form-item>
                <el-form-item label="所在地区：" prop="area" style="width: 500px">
                    <el-input v-model="form.area" placeholder="请输入所在地区"></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="remark" style="width: 500px">
                    <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="form.remark"> </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onSubmit('form')" :loading="loading">保存</el-button>
                </el-form-item>
            </el-form>
        </el-row>
    </div>
</template>

<script>
    import normalFilter from "@/components/NormalFilter";
    import { add, update } from "@/api/customer";
    import Tinymce from "@/components/Tinymce";
    import { Message } from "element-ui";
    import { CUSTOMERTYPE } from "@/utils/enumConfig";

    export default {
        name: "CustomerAdd",
        components: {
            normalFilter,
            Tinymce,
        },
        props: {
            info: {
                type: Object,
                default: {
                    name: "",
                    source: "",
                    addWxWork: "0",
                    area: "",
                    phone: "",
                    idcardNo: "",
                    remark: "",
                },
            },
        },
        data() {
            const _checkPhone = (rule, value, callback) => {
                if (!/^1[3456789]\d{9}$/.test(value)) {
                    return callback(new Error("手机号格式错误"));
                }
                callback();
            };
            return {
                loading: false,
                CUSTOMERTYPE,
                form: this.info,
                rules: {
                    name: [{ required: true, message: "姓名不能为空" }],
                    area: [{ required: true, message: "所在地区不能为空" }],
                    addWxWork: [{ required: true, message: "选项不能为空" }],
                    phone: [
                        { required: true, message: "手机号不能为空" },
                        { validator: _checkPhone, trigger: "blur" },
                    ],
                },
            };
        },
        mounted() {
            //   tinymce.init({})
        },
        methods: {
            onSubmit(formName) {
                const { validate } = this.$refs[formName];
                let _this = this;
                validate((valid) => {
                    if (valid) {
                        this._submitData(this.form);
                    }
                });
            },
            async _submitData(params) {
                this.loading = true;
                let id = this.info.id;
                let res = !id ? await add(params) : await update({ ...params, id });
                this.loading = false;
                if (res.code === 1) {
                    Message({
                        message: "保存成功！",
                        type: "success",
                        duration: 1 * 1000,
                    });
                    setTimeout(() => {
                        this.$router.go(-1);
                    }, 1000);
                }
            },
        },
        watch: {
            info(newV, oldV) {
                this.form = newV;
            },
        },
    };
</script>

<style lang="scss" scoped>
    .container {
        width: 100%;
        height: 100%;
        padding: 20px;
        .radio-user {
            ::v-deep .el-form-item__error {
                left: 5%;
            }
        }
    }
</style>
